// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var css_detail_info = require("../../../html/platform/financing/financingDetailInfo.css");
	var css_detail_info1 = require("../../../html/platform/financing/financingManage.css");
	var tools = require("tools");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var constant = require("constant");
	var pageIndex = 1,
		pageSize = 10000,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var deliveryNoticeId;
	var noticeState;
    var left;
	var deliveryNoticeDetail;
	var clerk;
	var init = function(page) {
		deliveryNoticeId = page.params.deliveryNoticeId || base.getUrlParam("deliveryNoticeId");
		noticeState = page.params.noticeState || base.getUrlParam("noticeState");
		left = base.getUrlParam("left");
		if(left==null){
			left=1;
		}
		
		if (left == 1) {
			$(".acceptTasks").removeClass("none");
			$(".signDelivery").addClass("none");
		} else if (left == 2) {
			$(".acceptTasks").addClass("none");
			$(".signDelivery").removeClass("none");
		} else {
			$(".acceptTasks").addClass("none");
			$(".signDelivery").addClass("none");
		}
		initEvent();
		inquireDeliveryNoticeDetail();
		router.init();
	};

	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击放货配送
		$(".deliveryList").on("click", function() {
			router.route("financing/deliveryList.html?deliveryNoticeId=" + deliveryNoticeId + "&noticeState=" + noticeState, true);
		});

		$("#clerkName").on("change", function() {
			$(".clerkNameInput").val($("#clerkName  option:selected").text());
			var clerkId = $("#clerkName").val();
			$.each(clerk.datas, function(i, val) {
				if (val.clerkId == clerkId) {
					$("#clerkMobile").val(val.clerkMobile);
				}
			});
		});

		//点击下发放货配送提交按钮
		$("#delivery_dialog").on("click", "#sure_deliveryNotice", function() {
			tools.protectDoubleClick();
			var inventoryIds = [];
			$.each($(".inventoryId:checked"), function(i, val) {
				inventoryIds[i] = val.value;
			});
			if (inventoryIds.length == 0) {
				base.alert(2, "请勾选货箱再下达放货配送！");
				return;
			}
			var clerkName = $(".clerkNameInput").val();
			var clerkId = $("#clerkName").val();
			var clerkMobile = $("#clerkMobile").val();
			base.fnAjax({
				action: "createDelivery.json",
				oData: {
					deliveryNoticeId: deliveryNoticeId,
					clerkMobile: clerkMobile,
					clerkId: clerkId,
					clerkName: clerkName,
					inventoryIds: inventoryIds
				}, //传递参数
				fnSuccess: function(data) {
					tools.cancelProtectDoubleClick();
					if (data.status == 200) {
						tools.cancelProtectDoubleClick();
						base.alert(1, "下达放货配送成功");
						dialog.hideDialog($("#delivery_dialog"));
						router.route("deliveryNotice/deliveryNoticeDetail.html?deliveryNoticeId=" + deliveryNoticeId + "&noticeState=" + noticeState, true);
					} else {
						base.alert(2, data.message);
					}
				}
			});

		});
	};
	//显示运输通知--货箱
	var showInventory = function() {
		base.fnAjax({
			action: "inquireWaitDeliveryBulkstockInventoryList.json",
			oData: {
				deliveryNoticeId: deliveryNoticeId
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#inventoryTpl").html();
					var html = juicer(tpl, data);
					$("#inventoryBody").html(html);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});


	}
	var inquireLogisticsClerkPageList = function() {
			base.fnAjax({
				action: "inquireLogisticsClerkPageList.json",
				oData: {
					pageIndex: pageIndex,
					pageSize: pageSize
				}, // 传递参数
				fnSuccess: function(data) {
					clerk = data;
					if (data.status == 200) {
						var tpl = $("#clerkTpl").html();
						var html = juicer(tpl, data);
						$("#clerkName").html(html);

					} else {
						base.alert(2, "信息获取失败");
					}
				}
			});
		}
		//获取放货通知明细(放货通知)
	var inquireDeliveryNoticeDetail = function() {
		base.fnAjax({
			action: "inquireDeliveryNoticeDetail.json",
			oData: {
				deliveryNoticeId: deliveryNoticeId
			}, // 传递参数
			fnSuccess: function(data) {
				deliveryNoticeDetail = data;
				if (data.status == 200) {
					var tpl = $("#financingDetail").html();
					var html = juicer(tpl, data);
					$("#deliveryDetlailMainContent").html(html);

				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});